home *** CD-ROM | disk | FTP | other *** search
/ PC Format (UK) 188 / 01-04 PC Format 188 [2006-06] DVD side 1_.iso / DiscContents / Workshops / Workshops_Games / Sphere / InstallSphere1.0.exe / games / p101r5 / scripts / menumod.js < prev    next >
Text File  |  2003-02-18  |  3KB  |  121 lines

  1. EvaluateSystemScript("colors.js");
  2.  
  3.  
  4. function Menu(){
  5.  
  6.     /*if (this instanceof Menu == false) {
  7.         return new Menu();
  8.     }*/
  9.  
  10.     // default properties
  11.  
  12.     this.font            = GetSystemFont()
  13.  
  14.     this.window_style    = GetSystemWindowStyle()
  15.  
  16.     this.arrow            = GetSystemArrow();
  17.  
  18.     this.up_arrow        = GetSystemUpArrow();
  19.     this.down_arrow        = GetSystemDownArrow();
  20.     this.escape_function    = function() { };
  21.     this.items            = new Array();
  22. }
  23.  
  24. // add item
  25. Menu.prototype.addItem = function(name, callback, color) {
  26.  
  27.   if (color == undefined) {
  28.     color = White;
  29.   }
  30.  
  31.   var item = new Object;
  32.   item.name     = name;
  33.   item.callback = callback;
  34.   item.color    = color;
  35.   this.items[this.items.length] = item;
  36. }
  37.  
  38. // execute
  39. Menu.prototype.execute = function(x, y, w, h) {
  40.  
  41.     var selection = 0;
  42.   with (this) {
  43.     var background = GrabImage(0, 0, GetScreenWidth(), GetScreenHeight());
  44.  
  45.     var text_height = font.getHeight();
  46.     var shown_items = Math.floor(h / text_height);
  47.  
  48.     var top_selection = 0;
  49.  
  50.     while (true) {
  51.       // draw background
  52.       background.blit(0, 0);
  53.  
  54.       // draw the window
  55.       window_style.drawWindow(x, y, w, h);
  56.  
  57.       // draw the menu items
  58.       for (var i = 0; i < shown_items; i++) {
  59.         if (i < items.length) {
  60.           font.setColorMask(Black);
  61.           font.drawText(x + 16 + 1, y + i * text_height + 1, items[i + top_selection].name);
  62.           font.setColorMask(items[i + top_selection].color);
  63.           font.drawText(x + 16,     y + i * text_height,     items[i + top_selection].name);
  64.         }
  65.       }
  66.  
  67.       // draw the selection arrow
  68.       arrow.blit(x, y + (selection - top_selection) * text_height);
  69.  
  70.       // draw the up and down arrows if necessary
  71.       if (top_selection > 0) {
  72.         up_arrow.blit(x + w - up_arrow.width, y);
  73.       }
  74.       if (top_selection + shown_items < items.length) {
  75.         down_arrow.blit(x + w - down_arrow.width, y + text_height * shown_items - down_arrow.height);
  76.       }
  77.  
  78.       FlipScreen();
  79.  
  80.       // handle keypresses
  81.       while (AreKeysLeft()) {
  82.         switch (GetKey()) {
  83.           case KEY_ENTER: {
  84.             var item = items[selection];
  85.             item.callback();
  86.             return(selection);
  87.           }
  88.  
  89.           case KEY_ESCAPE: {
  90.             escape_function();
  91.             return(selection);
  92.           }
  93.  
  94.           case KEY_DOWN: {
  95.             if (selection < items.length - 1) {
  96.               selection++;
  97.               if (selection >= top_selection + shown_items) {
  98.                 top_selection++;
  99.               }
  100.             }
  101.             break;
  102.           }
  103.  
  104.           case KEY_UP: {
  105.             if (selection > 0) {
  106.               selection--;
  107.               if (selection < top_selection) {
  108.                 top_selection--;
  109.               }
  110.             }
  111.             break;
  112.           }
  113.  
  114.         }
  115.       } // end handle input
  116.     }
  117.  
  118.   } // end with
  119.     return(selection);
  120. }
  121.